package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.NetworkUtils;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityWebContrastBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.IpUtil;
import stark.common.basic.utils.RxUtil;

public class WebContrastActivity extends BaseAc<ActivityWebContrastBinding> {

    private boolean isTest = false;

    @Override
    protected int onCreate() {
        return R.layout.activity_web_contrast;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivWebContrastBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvExecute.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        if (isTest) return;
        switch (v.getId()) {
            case R.id.tvExecute://执行/刷新信息
                isTest = true;
                showDialog(getString(R.string.query_ing));
                startQuery1("www.baidu.com");
                break;
        }
    }

    @Override
    protected void initData() {

    }

    //开始查询
    private void startQuery1(String url) {
        RxUtil.create(new RxUtil.Callback<String>() {

            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                String ip = NetworkUtils.getDomainAddress(url);
                IpUtil.IpMaskInfo maskInfo = IpUtil.ipMask(ip, 8);
                setResult(ip, maskInfo, mDataBinding.tvDdText1, mDataBinding.tvDdText2, mDataBinding.tvDdText3,
                        mDataBinding.tvDdText4, mDataBinding.tvDdText5, mDataBinding.tvDdText6, mDataBinding.tvDdText7);
                emitter.onNext("");
            }

            @Override
            public void accept(String kind) {
                startQuery2("www.jd.com");
            }
        });
    }

    //开始查询
    private void startQuery2(String url) {
        RxUtil.create(new RxUtil.Callback<String>() {

            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                String ip = NetworkUtils.getDomainAddress(url);
                IpUtil.IpMaskInfo maskInfo = IpUtil.ipMask(ip, 8);
                setResult(ip, maskInfo, mDataBinding.tvJdText1, mDataBinding.tvJdText2, mDataBinding.tvJdText3,
                        mDataBinding.tvJdText4, mDataBinding.tvJdText5, mDataBinding.tvJdText6, mDataBinding.tvJdText7);
                emitter.onNext("");
            }

            @Override
            public void accept(String kind) {
                startQuery3("www.tmall.com");
            }
        });
    }

    //开始查询
    private void startQuery3(String url) {
        RxUtil.create(new RxUtil.Callback<String>() {

            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                String ip = NetworkUtils.getDomainAddress(url);
                IpUtil.IpMaskInfo maskInfo = IpUtil.ipMask(ip, 8);
                setResult(ip, maskInfo, mDataBinding.tvTmText1, mDataBinding.tvTmText2, mDataBinding.tvTmText3,
                        mDataBinding.tvTmText4, mDataBinding.tvTmText5, mDataBinding.tvTmText6, mDataBinding.tvTmText7);
                emitter.onNext("");
            }

            @Override
            public void accept(String kind) {
                startQuery4("www.mi.com");
            }
        });
    }

    //开始查询
    private void startQuery4(String url) {
        RxUtil.create(new RxUtil.Callback<String>() {

            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                String ip = NetworkUtils.getDomainAddress(url);
                IpUtil.IpMaskInfo maskInfo = IpUtil.ipMask(ip, 8);
                setResult(ip, maskInfo, mDataBinding.tvXmText1, mDataBinding.tvXmText2, mDataBinding.tvXmText3,
                        mDataBinding.tvXmText4, mDataBinding.tvXmText5, mDataBinding.tvXmText6, mDataBinding.tvXmText7);
                emitter.onNext("");
            }

            @Override
            public void accept(String kind) {
                isTest = false;
                dismissDialog();
            }
        });
    }

    //设置结果
    private void setResult(String ip, IpUtil.IpMaskInfo maskInfo
            , TextView tv1, TextView tv2, TextView tv3, TextView tv4, TextView tv5, TextView tv6, TextView tv7) {
        tv1.setText(ip);
        tv2.setText(maskInfo.mask);
        tv3.setText(maskInfo.startIp + "");
        tv4.setText(maskInfo.broadcastIp);
        tv5.setText(maskInfo.firstUseIp);
        tv6.setText(maskInfo.lastUseIp);
        tv7.setText(maskInfo.usableIpCount + "");
    }

}